//
//  HMEmoticonKeyboardView.swift
//  WeiBo
//
//  Created by Feng on 2017/6/14.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

class HMEmoticonKeyboardView: UIView {

    //MARK: --懒加载控件
    fileprivate lazy var emoticonToolBar: HMEmoticonToolBarView =
    {
        let toolBarView = HMEmoticonToolBarView()
        
        return toolBarView
    }()
    
    //collectionView
    fileprivate lazy var emoticonCollectionView: UICollectionView =
    {
        let flowLayout = UICollectionViewFlowLayout()
        let collectionViewSize = CGRect(origin: CGPoint.zero, size: CGSize(width: self.width, height: self.height - 35))
        let emoticonView: UICollectionView = UICollectionView(frame: collectionViewSize, collectionViewLayout: flowLayout)
        
        flowLayout.itemSize = CGSize(width: self.width, height: self.height - 35)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = .horizontal
        emoticonView.bounces = false
        emoticonView.showsVerticalScrollIndicator = false
        emoticonView.showsHorizontalScrollIndicator = false
        emoticonView.isPagingEnabled = true
        emoticonView.backgroundColor = self.backgroundColor
        
        emoticonView.dataSource = self
        emoticonView.delegate = self
        emoticonView.register(HMEmoticonViewCell.self, forCellWithReuseIdentifier: HMEmoticonKeyboardIdentifier)
        
        return emoticonView
    }()
    
    //pageControl 分页指示器
    fileprivate lazy var pageControl: UIPageControl =
    {
        /*
         通过运行时获取的私有成员变量
         _currentPageImage
         @"UIImage"
         _pageImage
         @"UIImage"
         */
        let pgControl = UIPageControl()
        
//        pgControl.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
//        pgControl.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        pgControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        pgControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pgControl.numberOfPages = 5
        pgControl.currentPage = 0
        pgControl.hidesForSinglePage = true
        
        return pgControl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: --运用运行时runtime获取成员属性
    private func getPropertyList()
    {
        var count: UInt32 = 0
        
        let propertyList =  class_copyPropertyList(UIPageControl.self, &count)
        
        for i in 0..<count
        {
            let propertyName = property_getName(propertyList?[Int(i)])
            let propertyType = property_getAttributes(propertyList?[Int(i)])
            
            let name = String(cString: propertyName!)
            let type = String(cString: propertyType!)
            
            print(name)
            print(type)
        }
    }
    private func getIvarList()
    {
        var count: UInt32 = 0
        let ivarList = class_copyIvarList(UIPageControl.self, &count)
        
        for i in 0..<count
        {
            let ivarName = ivar_getName(ivarList?[Int(i)])
            let ivarType = ivar_getTypeEncoding(ivarList?[Int(i)])
            
            let name = String(cString: ivarName!)
            let type = String(cString: ivarType!)
            
            print(name)
            print(type)
        }
    }
    
    //MARK: --设置界面及约束
    private func setupUI()
    {
        //测试
        getIvarList()
        
        //  默认滚动到第二组表情数据
        let normalIndexPath = IndexPath(item: 0, section: 1)
        emoticonCollectionView.scrollToItem(at: normalIndexPath, at: [], animated: false)
        // 同时设置pageControl
        setPageControl(indexPath: normalIndexPath)
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(emoticonToolBar)
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        
        emoticonToolBar.snp.makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        pageControl.snp.makeConstraints { (make) in
            make.bottom.equalTo(emoticonCollectionView)
            make.centerX.equalTo(emoticonCollectionView)
            make.height.equalTo(10)
        }
        
        emoticonToolBar.emoticonToolBarCallBack = { (type) -> () in

            let indexPath: IndexPath
            
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .lxh:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            
            self.emoticonCollectionView.scrollToItem(at: indexPath, at: [], animated: false)
        }
    }
    
    //MARK: --刷新最新数据
    func reloadRecentData()
    {
        let indexPath = IndexPath(item: 0, section: 0)
        
        emoticonCollectionView.reloadItems(at: [indexPath])
    }
}

//MARK: --collectionView 数据源方法
extension HMEmoticonKeyboardView: UICollectionViewDataSource, UICollectionViewDelegate
{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticonArray[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HMEmoticonKeyboardIdentifier, for: indexPath) as! HMEmoticonViewCell
        
        cell.modelArray = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section][indexPath.item]
        
//        cell.backgroundColor = RandomColor()
        cell.indexPath = indexPath
        
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //获取内容中心点
        let contentCenterX = scrollView.contentOffset.x + scrollView.width * 0.5
        let contentCenterY = scrollView.contentOffset.y + scrollView.height * 0.5
        
        let contentCenter = CGPoint(x: contentCenterX, y: contentCenterY)
        
        //根据中心点获取indexPath
        if let indexPath = emoticonCollectionView.indexPathForItem(at: contentCenter)
        {
            let section = indexPath.section
            
            emoticonToolBar.selectedButtonWithSection(section: section)
            //设置pageControl
            setPageControl(indexPath: indexPath)
        }
    }
    
    //pageControl设置当前页
    fileprivate func setPageControl(indexPath: IndexPath)
    {
        pageControl.numberOfPages = HMEmoticonTools.sharedTools.allEmoticonArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
}
